/*
 * @(#)DmsPublicDocument.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.action;

import java.sql.Connection;

import org.quartz.JobDataMap;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.IndexBackgroundThread;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.EventLogger;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.IndexManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.workflow.WorkflowErrorConstant;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.core.WorkflowProcessor;

/**
 * DmsPublicDocument.java
 *
 * This class is the automatic task handler for publish public document.
 *
 * @author      Angus Shiu
 * @company     DCIVision Limited
 * @creation date   26/01/2003
 * @version     $Revision: 1.12.2.1 $
 */

public class DmsPublicDocument extends com.dcivision.workflow.core.WorkflowProcessor {

  public static final String REVISION = "$Revision: 1.12.2.1 $";

  public DmsPublicDocument() {
    this.numOfRoutine = 1;
    this.labelType = WorkflowProcessor.LABEL_TYPE_USER_DEFINE;
    this.labelString = new String[] {"workflow.label.step_status_G"};
  }

  public synchronized void execute(JobDataMap dataMap, WorkflowProgress currentProgress) throws ApplicationException {
    log.info("***** DmsPublicDocument workflow processor BEGIN *****");
    Connection conn = null;

    Integer trigerEventUserID = new Integer(0);
    String serverName = "127.0.0.1";
    String functionName = SystemFunctionConstant.WORKFLOW_TASK;
    String errorMessage = "";
    try {
      //* get the DB Connection
      SessionContainer sessionCon = this.getSessionContainer();

      /*************************************************************
       *  get Event Log Information Begin
       *************************************************************/
      if (!Utility.isEmpty(sessionCon.getUserRecordID())) {
        trigerEventUserID = sessionCon.getUserRecordID();
      }

      if (!Utility.isEmpty(sessionCon.getUserIPAddress())) {
        serverName = sessionCon.getUserIPAddress();
      }
      errorMessage = "DmsPublicDocument.java -- Track ID: " + currentProgress.getTrackID() +
                     ", Workflow Progress ID : " + currentProgress.getID() +
                     ", Workflow Step ID: " + currentProgress.getWorkflowStepID() +
                     "<br> ";
      /*************************************************************
       *  get Event Log Information End
       *************************************************************/
      //for schedule job useing the processer connection.
      isLocalConnection = false;
      if(!Utility.isEmpty(this.getConnection())){
        conn = this.getConnection();
      }else{
        isLocalConnection = true;
        conn = DataSourceFactory.getConnection();
      }

      //* create required DAObject
      DmsDocumentDAObject docDAO = new DmsDocumentDAObject(sessionCon, conn);

      //* get relevant information from
      AbstractBaseObject workflowObject = (AbstractBaseObject) dataMap.get("WORKFLOW_OBJECT");
      log.debug("workflow object class name: "+workflowObject.getClass().getName());
      Integer docID = workflowObject.getID();
      //docID = new Integer(-11);
      //* do what you want below
      docDAO.updateStatusByObjectID(docID, GlobalConstant.STATUS_ACTIVE);

      //* after the execution, set the desired appropriate out number
      this.returnRoutineIndex = 1; //* should be between 0 and numOfRoutine-1
      //* if need to auto-Execute the following step, set the below variable to true (default false)
      this.bAutoExecuteNextStep=false; this.bCreateNextWorkflowProgress=true;

      //update thee documet index status
      DmsDocument tmpDmsDocument =(DmsDocument) docDAO.getObjectByID(docID);
      IndexBackgroundThread ibt = new IndexBackgroundThread(tmpDmsDocument, null, GlobalConstant.OP_MODE_UPDATE, new String[]{IndexManager.INDEX_NAME_RECORD_STATUS});
      ibt.start();

      if(isLocalConnection){
        conn.commit();
      }
    } catch (ApplicationException appEx) {
      log.error("Application exception:\n"+appEx);
      /***********************************
       *  Record the Event Log.
       ***********************************/
      EventLogger.logEventMessage(trigerEventUserID, serverName, errorMessage, appEx.getMsgCode(), WorkflowErrorConstant.LOG_AUTO_TASK_ERROR_CODE, functionName, appEx.toString() );

      throw appEx; //* prop
    } catch (Exception e) {
      log.error("Error during processing this automatic task.", e);
      /***********************************
       *  Record the Event Log.
       ***********************************/
      EventLogger.logEventMessage(trigerEventUserID, serverName, errorMessage, ErrorConstant.COMMON_FATAL_ERROR, WorkflowErrorConstant.LOG_AUTO_TASK_ERROR_CODE, functionName, e.toString() );

      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e); //* throw an applicationExcpetion with an appropriate
    } finally {
      if(isLocalConnection){
        try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      }
    }

    log.info("***** DmsPublicDocument workflow processor END   *****");
  }

}
